<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>父子子父间通信</title>
</head>

<body>
  <div id="app">
    <cpn :number1='num1' :number2='num2' @num1change='num1Change' @num2change='num2Change'></cpn>
  </div>
  <template id="cpn">
    <div>
      <h3>props{{number1}}</h3>
      <p>data{{dnumber1}}</p>
      <input type="text" :value='dnumber1' @input='num1Input'>
      <h3>props{{number2}}</h3>
      <p>data{{dnumber2}}</p>
      <input type="text" :value='dnumber2' @input='num2Input'>
    </div>
  </template>
  <script src="../../vue.js"></script>
  <script>
    new Vue({
      el: '#app',
      data: {
        num1: 0,
        num2: 1
      },
      // 之前父组件数据不更新 TODO 改了自定义事件名就好了 难道自定义事件不能使用驼峰命名
      methods: {
        num1Change(value) {
          this.num1 = value;
        },
        num2Change(value) {
          this.num2 = value;
        }
      },
      components: {
        cpn: {
          template: '#cpn',
          props: {
            number1: Number,
            number2: Number,
          },
          data() {
            return {
              dnumber1: this.number1,
              dnumber2: this.number2
            }
          },
          methods: {
            num1Input(event) {
              this.dnumber1 = parseInt(event.target.value)
              this.$emit('num1change', this.dnumber1)
              // 同时修改dnumber2数据
              this.dnumber2 = this.dnumber1 * 100
              this.$emit('num2change', this.dnumber2)
            },
            num2Input(event) {
              this.dnumber2 = parseInt(event.target.value)
              this.$emit('num2change', this.dnumber2)
              // 同时修改dnumber1数据
              this.dnumber1 = this.dnumber2 / 100
              this.$emit('num1change', this.dnumber1)
            },
          }
        }
      }
    })
  </script>
</body>

</html>